package com.hmy.uaa.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmy.uaa.mapper.UserRoleRelationMapper;
import com.hmy.uaa.po.UserRoleRelation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class UserRoleRelationService {


    @Autowired
    private UserRoleRelationMapper userRoleRelationMapper;


    /**
     * 查询用户
     *
     * @param userId 用户id
     * @return User 返回用户对象
     */
    public List<UserRoleRelation> selectList(Long userId) {

        LambdaQueryWrapper<UserRoleRelation> queryWrapper = Wrappers.<UserRoleRelation>lambdaQuery()
                .eq(UserRoleRelation::getUserId, userId);

        return userRoleRelationMapper.selectList(queryWrapper);
    }

    /**
     * 新增角色
     *
     * @param userId 用户id
     * @param roleId 角色id
     */
    public void save(Long userId, Long roleId) {

        UserRoleRelation userRoleRelation = new UserRoleRelation();
        userRoleRelation.setUserId(userId);
        userRoleRelation.setRoleId(roleId);
        userRoleRelation.setCreateTime(LocalDateTime.now());
        userRoleRelationMapper.insert(userRoleRelation);

    }

    /**
     * 修改角色
     *
     * @param userId 用户id
     * @param roleId 角色id
     */
    public void update(Long userId, Long roleId) {


        //修改角色
        UserRoleRelation userRoleRelation = new UserRoleRelation();
        userRoleRelation.setUserId(userId);
        userRoleRelation.setRoleId(roleId);
        userRoleRelation.setUpdateTime(LocalDateTime.now());

        LambdaQueryWrapper<UserRoleRelation> queryWrapper = Wrappers.<UserRoleRelation>lambdaQuery()
                .eq(UserRoleRelation::getUserId, userId);

        userRoleRelationMapper.update(userRoleRelation, queryWrapper);

    }

    /**
     * 删除角色
     *
     * @param id 请求参数
     */
    public void delete(Long id) {
        LambdaQueryWrapper<UserRoleRelation> queryWrapper = Wrappers.<UserRoleRelation>lambdaQuery()
                .eq(UserRoleRelation::getUserId, id);
        userRoleRelationMapper.delete(queryWrapper);
    }

}
